<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>修改</title>
    <link rel="stylesheet" href="/component/pear/css/pear.css" />
    <style>
        .inputTxt {
            margin-left: 10px;
        }
    </style>
</head>

<body>
    <form id="set-user" class="layui-form layui-form-pane" action="" lay-filter="set-user">
        <div class="mainBox">
            <div class="main-container">
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">账号</label>
                    <div class="layui-input-block">
                        <div class="layui-form-mid inputTxt">
                            <?php echo $username ?>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">昵称</label>
                    <div class="layui-input-block">
                        <input type="text" name="nickname" autocomplete="off" class="layui-input"
                            value="<?php echo $nickname ?>">
                    </div>
                </div>
                <div class="layui-form-item" pane>
                    <label class="layui-form-label">密码</label>
                    <div class="layui-input-block">
                        <div class="layui-form-mid inputTxt" style="float: none;">
                            ********
                            <a href="javascript:;" lay-on="changePw" style="float: right;" title="修改密码"><span
                                    class="icon pear-icon pear-icon-edit"></span></a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit=""
                    lay-filter="user-save">
                    <i class="layui-icon layui-icon-ok"></i>
                    提交
                </button>
                <button type="reset" class="pear-btn pear-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i>
                    重置
                </button>
            </div>
        </div>
    </form>
    <script type="text/html" id="changePassWord">
        <div class="main-container">
            <form class="layui-form layui-form-pane">
              <div class="layui-form-item">
                <label class="layui-form-label">原密码</label>
                <div class="layui-input-block">
                  <input type="password" name="old_password" required lay-verify="required" placeholder="请输入原密码"
                    autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">新密码</label>
                <div class="layui-input-block">
                  <input type="password" name="new_password" required lay-verify="required" placeholder="请输入新密码"
                    autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <label class="layui-form-label">确认密码</label>
                <div class="layui-input-block">
                  <input type="password" name="confirm_password" required lay-verify="required" placeholder="请再次输入新密码"
                    autocomplete="off" class="layui-input">
                </div>
              </div>
              <div class="layui-form-item">
                <div class="layui-input-block">
                  <button class="layui-btn" lay-submit lay-filter="changePassWord">确认修改</button>
                </div>
              </div>
            </form>
          </div>
    </script>
    <script src="/component/layui/layui.js"></script>
    <script src="/component/pear/pear.js"></script>
    <script>
        layui.use(['form'], function () {
            let form = layui.form;
            let util = layui.util;

            form.on('submit(user-save)', function (obj) {
                let field = obj.field;
                (async () => {
                    const url = '/api/User/editUser';
                    const info = field;
                    const res = await fetch(url, { method: 'post', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ nickname: info.nickname }) });
                    if (res.status !== 200) {
                        layer.msg('请求失败，请重试！', { tips: 2, time: 1200 });
                        return false;
                    }
                    const rec_info = await res.json();
                    if (rec_info.code === 0) {
                        layer.msg(rec_info.msg, { icon: 1, time: 1200 }, () => {
                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                            parent.location.reload();
                        });
                    } else {
                        layer.msg(rec_info.msg, { icon: 2, time: 1200 });
                    }
                })()
                return false;
            });

            util.on({
                'changePw': (ele) => {
                    layer.open({
                        type: 1,
                        area: '350px',
                        resize: false,
                        shadeClose: true,
                        title: '修改密码',
                        content: document.getElementById('changePassWord').innerHTML,
                        success: () => {
                            form.on('submit(changePassWord)', (data) => {
                                var field = data.field;
                                // 检查新密码和确认密码是否一致
                                if (field.new_password !== field.confirm_password) {
                                    layer.msg('新密码和确认密码不一致，请重新输入', { icon: 2 });
                                    return false;
                                }
                                // 密码修改
                                (async () => {
                                    const url = '/api/User/changePassword';
                                    const info = { old_password: field.old_password, new_password: field.new_password };
                                    const res = await fetch(url, {
                                        method: 'post',
                                        headers: { 'Content-Type': 'application/json' },
                                        body: JSON.stringify(info)
                                    });
                                    if (res.status !== 200) {
                                        layer.msg('请求失败，请重试！', { tips: 2, time: 1200 });
                                        return false;
                                    }
                                    const rec_info = await res.json();
                                    if (rec_info.code === 0) {
                                        layer.msg(rec_info.msg, { icon: 1, time: 1200 }, () => {
                                            parent.layer.close(parent.layer.getFrameIndex(window.name)); //关闭当前页
                                            parent.location.reload();
                                        });
                                    } else {
                                        layer.msg(rec_info.msg, { icon: 2, time: 1200 });
                                    }
                                })()
                                return false;
                            });
                        }
                    });
                }
            });
        })
    </script>
</body>

</html>